package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Examine;
import com.ruoyi.system.domain.InboundTouristTotalStatistics;
import com.ruoyi.system.domain.UnitInformation;
import com.ruoyi.system.domain.vo.FileUploadVO;
import com.ruoyi.system.mapper.ExamineMapper;
import com.ruoyi.system.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InboundSubsidyMapper;
import com.ruoyi.system.domain.InboundSubsidy;
import com.ruoyi.system.service.IInboundSubsidyService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 邀请入境考察补贴申请，存储补贴申请信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-22
 */
@Service
public class InboundSubsidyServiceImpl extends ServiceImpl<InboundSubsidyMapper, InboundSubsidy> implements IInboundSubsidyService {
    @Autowired
    private InboundSubsidyMapper inboundSubsidyMapper;

    @Resource
    private FileUploadService fileUploadService;

    @Resource
    private ExamineMapper examineMapper;

    /**
     * 查询邀请入境考察补贴申请，存储补贴申请信息
     *
     * @param id 邀请入境考察补贴申请，存储补贴申请信息主键
     * @return 邀请入境考察补贴申请，存储补贴申请信息
     */
    @Override
    public InboundSubsidy selectInboundSubsidyById(Long id) {

        InboundSubsidy inboundSubsidy = inboundSubsidyMapper.selectInboundSubsidyById(id);
        List<FileUploadVO> list = fileUploadService.listFileUploadsByBusinessId(inboundSubsidy.getBusinessId());
        inboundSubsidy.setFiles(list);
        List<Examine> examines = inboundSubsidy.getExamines();
        examines.sort((e1, e2) -> e2.getCreateTime().compareTo(e1.getCreateTime()));
        return inboundSubsidy;
    }

    /**
     * 查询邀请入境考察补贴申请，存储补贴申请信息列表
     *
     * @param inboundSubsidy 邀请入境考察补贴申请，存储补贴申请信息
     * @return 邀请入境考察补贴申请，存储补贴申请信息
     */
    @Override
    public List<InboundSubsidy> selectInboundSubsidyList(InboundSubsidy inboundSubsidy) {
        startPage();
        List<InboundSubsidy> inboundTouristTotalStatisticsList  = new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            // 可根据业务需求记录日志或抛出异常
            inboundTouristTotalStatisticsList = Collections.emptyList();
        } else {
            QueryWrapper<InboundSubsidy> inboundSubsidyQueryWrapper = new QueryWrapper<>(inboundSubsidy);
            inboundSubsidyQueryWrapper.orderByDesc("created_at");
            if (!SecurityUtils.isAdmin(userId)) {
                inboundSubsidyQueryWrapper.eq("create_by", userId); // 使用 Long 类型进行查询
            }
            inboundTouristTotalStatisticsList = inboundSubsidyMapper.selectList(inboundSubsidyQueryWrapper);
        }
        // 添加权限过滤
//        Long userId = SecurityUtils.getUserId();
//        if (!SecurityUtils.isAdmin(userId)) {
//            inboundSubsidy.setCreateBy(userId.toString());
//        }
//        QueryWrapper<InboundSubsidy> inboundSubsidyQueryWrapper = new QueryWrapper<>(inboundSubsidy);
//        List<InboundSubsidy> inboundTouristTotalStatisticsList  = new ArrayList<>();
//        inboundSubsidyQueryWrapper.orderByDesc("created_at");
//        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
//         inboundTouristTotalStatisticsList = inboundSubsidyMapper.selectList(inboundSubsidyQueryWrapper);
//        } else {
//            inboundSubsidyQueryWrapper.eq("create_by", SecurityUtils.getUserId().toString());
//            inboundTouristTotalStatisticsList = inboundSubsidyMapper.selectList(inboundSubsidyQueryWrapper);
//        }
//        if(inboundTouristTotalStatisticsList.size()>0){
//            inboundTouristTotalStatisticsList.forEach(row -> {
//                List<Examine> examines = row.getId()  == null ? new ArrayList<>() : examineMapper.selectList(new QueryWrapper<Examine>().eq("business_id", row.getId()) .eq(StringUtils.isNotEmpty(row.getStatus()), "status", row.getStatus()).orderByDesc("create_time"));
//                row.setExamines(examines);
//            });
//        }
        return inboundTouristTotalStatisticsList;
    }


    /**
     * 新增邀请入境考察补贴申请，存储补贴申请信息
     *
     * @param inboundSubsidy 邀请入境考察补贴申请，存储补贴申请信息
     * @return 结果
     */
    @Override
    public int insertInboundSubsidy(InboundSubsidy inboundSubsidy) {
        inboundSubsidy.setCreateBy(SecurityUtils.getUserId().toString());
        inboundSubsidy.setStatus(0);
        inboundSubsidy.setCreatedAt(new Date());
        int insert = inboundSubsidyMapper.insert(inboundSubsidy);

        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(inboundSubsidy.getId())).setStatus(0).setType(inboundSubsidy.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(inboundSubsidy.getRemark());
        examineMapper.insert(examine);
        return insert;
    }

    /**
     * 修改邀请入境考察补贴申请，存储补贴申请信息
     *
     * @param inboundSubsidy 邀请入境考察补贴申请，存储补贴申请信息
     * @return 结果
     */
    @Override
    public int updateInboundSubsidy(InboundSubsidy inboundSubsidy) {
        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(inboundSubsidy.getId())).setStatus(inboundSubsidy.getStatus()).setType(inboundSubsidy.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(inboundSubsidy.getRemark());
        examineMapper.insert(examine);
        return inboundSubsidyMapper.updateById(inboundSubsidy);
    }

    /**
     * 批量删除邀请入境考察补贴申请，存储补贴申请信息
     *
     * @param ids 需要删除的邀请入境考察补贴申请，存储补贴申请信息主键
     * @return 结果
     */
    @Override
    public int deleteInboundSubsidyByIds(Long[] ids) {
        return inboundSubsidyMapper.deleteInboundSubsidyByIds(ids);
    }

    /**
     * 删除邀请入境考察补贴申请，存储补贴申请信息信息
     *
     * @param id 邀请入境考察补贴申请，存储补贴申请信息主键
     * @return 结果
     */
    @Override
    public int deleteInboundSubsidyById(Long id) {
        return inboundSubsidyMapper.deleteInboundSubsidyById(id);
    }
}
